#include "SceneObject.h"

using namespace Ogre;

SceneObject::SceneObject(SceneNode* on, Camera *cam, RaySceneQuery* ray){
	objectNode=on;
	mCamera=cam;
	CEGUI::MouseCursor::getSingleton().setVisible(true);
	mRaySceneQuery = ray;    
}

SceneObject::~SceneObject(){
}

void SceneObject::stopObject(){
	
	if(objectNode)
		objectNode->showBoundingBox(false);
}


void SceneObject::moveObject(){

    Ray mouseRay = mCamera->getCameraToViewportRay(0.5f, 0.5f);

	mRaySceneQuery->setRay(mouseRay);
	//mRaySceneQuery->setSortByDistance(true);
	RaySceneQueryResult &result = mRaySceneQuery->execute();
	RaySceneQueryResult::iterator itr = result.begin( );

	// Get results, create a node/entity on the position
	if (itr != result.end() && itr->worldFragment)
	{
		objectNode->setPosition(itr->worldFragment->singleIntersection.x,itr->worldFragment->singleIntersection.y+objectNode->_getWorldAABB().getSize().y/2,itr->worldFragment->singleIntersection.z);
		
	}
	
}